package com.yunlin.utils;


import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class SecureUtils{
    
    
    /*
     * checksum= F(key) string F(string key){ string mac = getMac(); string salt
     * = getSalt(key, 0, 0); string sum = mac + salt;//字符串拼接 string md5 =
     * md5(sum); return md5; } string getSalt(string key, int n, int length){
     * string curChar = key[n]; length++; if(length == 8){ return curChar; }
     * return curChar + getSalt(key, (int)curChar, length); }
     */
    public static String encode(String key, String mac){
        String salt=getSalt(key, 0, 0);
        String sum=mac + salt;
        
        try {
            MessageDigest msgDigest=MessageDigest.getInstance("MD5");
            byte[] checksum=msgDigest.digest(sum.getBytes());
            StringBuilder sb=new StringBuilder();
            for (byte b : checksum) {
                int k=b;
                k=0x00ff & k;
                String hex=Integer.toHexString(k);
                if (hex.length() == 1) {
                    sb.append("0");
                }
                sb.append(hex);
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            return "";
        }
        
    }
    
    public static String getSalt(String key, int n, int length){
        n=n % key.length();
        String curChar="" + key.charAt(n);
        length++;
        if (length == 8) {
            return curChar;
        }
        return curChar + getSalt(key, (int) key.charAt(n), length);
    }
    
    
    public static byte[] encrypt(byte[] key, byte[] clear) throws Exception{
        SecretKeySpec skeySpec=new SecretKeySpec(key, "AES");
        Cipher cipher=Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted=cipher.doFinal(clear);
        return encrypted;
    }
    
    
    public static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception{
        SecretKeySpec skeySpec=new SecretKeySpec(raw, "AES");
        Cipher cipher=Cipher.getInstance("AES/ECB/NoPadding");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] decrypted=cipher.doFinal(encrypted);
        return decrypted;
    }
    
    
    public static byte checkcode(byte[] a){
        byte c=a[0];
        for (int i=0; i < a.length - 1; i++) {
            c=(byte) (c ^ a[i + 1]);
        }
        return c;
    }
    
    public static byte[] encryptXOR(byte[] key, byte[] clear) throws Exception{
        byte[] encrypted=new byte[16];
        for (int i=0; i < 16; i++) {
            encrypted[i]=(byte) (key[i] ^ clear[i]);
        }
        return encrypted;
    }
    
    public static byte[] decryptXOR(byte[] key, byte[] encrypted) throws Exception{
        byte[] decrypted=new byte[16];
        for (int i=0; i < 16; i++) {
            decrypted[i]=(byte) (key[i] ^ encrypted[i]);
        }
        return decrypted;
    }
    
    public static void main(String[] args) throws InterruptedException, Exception{//ffadd900

    }
}
